package com.example.sunlight.interneteverything.View;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.sunlight.interneteverything.R;

import data.Datas;

/**
 * Created by SunLight on 2017/8/8.
 */

public class EnvironmentView extends View {
    Bitmap one, two, three, four, five;
    Resources resources;
    UpDown upDown;
    int i = 0;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x123:
                    EnvironmentView.this.invalidate();
                    break;
                case 0x456:
                    if (i < height.length) {
                        new UpDown(i++).start();

                    } else {
                        removeCallbacks(runnable);
                    }
                    break;
            }
        }
    };
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            handler.sendEmptyMessage(0x456);
            handler.postDelayed(this, 200);
        }
    };

    public EnvironmentView(Context context) {
        super(context);
        init();
        handler.postDelayed(runnable, 100);
    }


    public void init() {
        resources = getResources();
        one = ((BitmapDrawable) resources.getDrawable(R.drawable.co2)).getBitmap();
        two = ((BitmapDrawable) resources.getDrawable(R.drawable.formaldehyde)).getBitmap();
        three = ((BitmapDrawable) resources.getDrawable(R.drawable.humidity)).getBitmap();
        four = ((BitmapDrawable) resources.getDrawable(R.drawable.temperature)).getBitmap();
        five = ((BitmapDrawable) resources.getDrawable(R.drawable.air)).getBitmap();
    }

    public EnvironmentView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public EnvironmentView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawRotateBitmap(canvas, null, three, 0, 1250, height[0]);
        drawRotateBitmap(canvas, null, four, 0, 600, height[2]);
        drawRotateBitmap(canvas, null, two, 0, 400, height[1]);
        drawRotateBitmap(canvas, null, one, 0, 1200, height[3]);
        drawRotateBitmap(canvas, null, five, 0, 600, 380);
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setAntiAlias(true);
        paint.setTextSize(25);
        drawText(Datas.Tem , 650, height[2] + 100, canvas, paint);
        drawText(Datas.Jia , 420, height[1] + 100, canvas, paint);
        drawText(Datas.Co2 , 1245, height[3] + 100, canvas, paint);
        paint.setTextSize(20);
        drawText(Datas.Hum , 1275, height[0] + 80, canvas, paint);
    }

    public void drawText(String text, float x, float y, Canvas canvas, Paint paint) {
        canvas.drawText(text, x, y, paint);
    }

    private void drawRotateBitmap(Canvas canvas, Paint paint, Bitmap bitmap, float rotation, float posX, float posY) {
        Matrix matrix = new Matrix();
        int offsetX = bitmap.getWidth() / 2;
        int offsetY = bitmap.getHeight() / 2;
        matrix.postTranslate(-offsetX, -offsetY);
        matrix.postRotate(rotation);
//        matrix.postScale(0.75f, 0.75f);
        matrix.postTranslate(posX + offsetX, posY + offsetY);
        canvas.drawBitmap(bitmap, matrix, paint);
    }


    int[] height = {150, 800, 200, 500};
    int[] noHe = {150, 800, 200, 500};
    boolean[] states = new boolean[height.length];


    class UpDown extends Thread {
        int i;

        public UpDown(int i) {
            this.i = i;
        }

        @Override
        public void run() {
            try {
                while (true) {
                    Thread.sleep(30);
                    if (states[i] == false) {
                        if (height[i] <= noHe[i] + 30) {
                            height[i]++;
                            handler.sendEmptyMessage(0x123);
                        } else {
                            states[i] = true;
                        }
                    } else {
                        if (height[i] >= noHe[i] - 30) {
                            height[i]--;
                            handler.sendEmptyMessage(0x123);
                        } else {
                            states[i] = false;
                        }
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
